package com.enterprise.support.utility;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Zhanggaojiang
 * Date: 11-1-19
 * Time: 上午10:34
 * Email: z82422@gmail.com
 * 数据格式化 对象，例如 日期 转换, 简单类型转换,换算
 */

public class DataFormat {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
    private static final SimpleDateFormat fullDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    private static final SimpleDateFormat cnDateFormat = new SimpleDateFormat("yyyy年MM月dd日");


    public static String getGMTString(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
        return fullDateFormat.format(date);
    }

    /**
     * 日期
     *
     * @param string
     * @return
     */
    public static Date StringFormtDate(String string) {
        if (Validation.isNull(string)) throw new RuntimeException("字符到日期的转换参数为空1");
        try {
            return dateFormat.parse(string);
        } catch (ParseException e) {
            throw new RuntimeException("字符到日期的转换错误,参数1:" + string);
        }
    }

    public static Date StringFormtDate(String format, String string) {
        if (Validation.isNull(format) || Validation.isNull(string)) throw new RuntimeException("字符到日期的转换参数为空2");
        try {
            return new SimpleDateFormat(format).parse(string);
        } catch (ParseException e) {
            throw new RuntimeException("字符到日期的转换错误,参数2:" + string);
        }
    }

    public static String StringFormtDateCN(Date date) {
        if (Validation.isNull(date)) throw new RuntimeException("Date日期到字符的转换参数为空!");
        return cnDateFormat.format(date);
    }

    public static String DateFormtString(Date date) {
        return dateFormat.format(date);
    }

    public static String DateFormtString(String format, Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static String DateFormatTime(Date date) {
        return timeFormat.format(date);
    }

    /**
     * 字符
     *
     * @param object
     * @return
     */
    public static String ObjectFormatString(Object object) {
        return (Validation.isNull(object)) ? "" : String.valueOf(object);
    }

    public static String ArrayFormtString(Object[] object) {
        if (Validation.isNull(object)) throw new RuntimeException("数组到字符串的转换参数为空");
        return String.valueOf(object[0]);
    }

    /*数字*/

    public static Float ObjectFormatFloat(Object object) {
        return (Validation.isNull(object)) ? 0f : Float.valueOf(object.toString());
    }

    public static Integer ObjectFormatInteger(Object object) {
        try {
            return (Validation.isNull(object)) ? 0 : Integer.valueOf(object.toString());
        } catch (Exception ex) {
            return 0;
        }
    }

    public static Long ObjectFormatLong(Object object) {
        return (Validation.isNull(object)) ? 0l : Long.valueOf(object.toString());
    }

    /**
     * 强制将String类型转换为Long类型;
     *
     * @param size
     * @return
     */
    public static Long getByteSize(String size) {
        String nuit = size.substring(size.length() - 1, size.length()).toUpperCase();
        if (nuit.equalsIgnoreCase("K")) {
            String num = size.substring(0, size.length() - 1);
            return Long.parseLong(num) * 1024;
        } else if (nuit.equalsIgnoreCase("M")) {
            String num = size.substring(0, size.length() - 1);
            return Long.parseLong(num) * 1024 * 1024;
        } else {
            return Long.parseLong(size);
        }
    }

    /**
     * 强制将String类型转换为Long类型;
     *
     * @param size
     * @return
     */
    public static String getByteCNM(Long size) {
        if (size == 0 || size < 1024) {
            return "1K";
        } else if (size / (1024) < 1024) {
            return (size / 1024) + "K";
        } else if (size / (1024 * 1024) < 1024) {
            return (size / (1024 * 1024)) + "M";
        } else {
            return (size / (1024 * 1024 * 1024)) + "G";
        }
    }

    /**
     * 日期
     *
     * @param object
     * @return
     */
    public static Date ObjectFormatDate(Object object) {
        return (Validation.isNull(object)) ? new Date() : (Date) object;
    }

    /**
     * 在当天的日期上加或减Number天后的 的日期
     *
     * @param dayNumber
     * @return
     */
    public static Date getDay(Integer dayNumber) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, dayNumber);
        calendar.set(Calendar.HOUR, 12);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    public static String ClassFormatFieldList(Class classType, Object entity) {
        List fields = new ArrayList();
        for (Method method : classType.getMethods()) {
            if (method.getModifiers() == Modifier.PUBLIC && method.getName().startsWith("get")) {
                try {
                    Object value = method.invoke(entity);
                    if (!Validation.isEmpty(value)) fields.add(method.getName() + "=" + value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return fields.size() > 0 ? fields.toString() : "此对象的所有属性值均为空！";
    }

    public static Date SimpleStringFormDate(String dataString) {
        try {
            StringBuffer strbuffer = new StringBuffer(dataString);
            strbuffer.insert(4, "-");
            strbuffer.insert(7, "-");
            if (!Validation.isDate(strbuffer.toString())) return null;
            return dateFormat.parse(strbuffer.toString());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 如果 value 是空则返回 defaults 默认值
     *
     * @param value
     * @param defaults
     * @return
     */
    public static Object getValueDefault(Object value, Object defaults) {
        return Validation.isEmpty(value) ? defaults : value;
    }

    public static String getStringDefault(String value, String defaults) {
        return Validation.isEmpty(value) ? defaults : value;
    }

    public static String getTimeDifference(String date) {
        try {
            return getTimeDifference(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date));
        } catch (ParseException ex) {
            ex.printStackTrace();
            return "日期错误";
        }
    }

    public static String getTimeDifference(Date date) {
        long timer = new Date().getTime() - date.getTime();
        if ((timer / 365 / 24 / 60 / 60 / 1000) > 0) {
            return (timer / 365 / 24 / 60 / 60 / 1000) + "年";
        } else if ((timer / 30 / 24 / 60 / 60 / 1000) > 0) {
            return (timer / 30 / 24 / 60 / 60 / 1000) + "个月";
        } else if ((timer / 24 / 60 / 60 / 1000) == 1) {
            return "昨天";
        } else if ((timer / 24 / 60 / 60 / 1000) == 2) {
            return "前天";
        } else if ((timer / 24 / 60 / 60 / 1000) > 2) {
            return (timer / 24 / 60 / 60 / 1000) + "天";
        } else if ((timer / 60 / 60 / 1000) > 0) {
            return (timer / 60 / 60 / 1000) + "小时";
        } else if ((timer / 60 / 1000) > 0) {
            return (timer / 60 / 1000) + "分钟";
        } else {
            return "1分钟";
        }
    }
}
